/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hadss_avoid_area/hadss_avoid_area.dart';

class AvoidAreaUI extends StatefulWidget {
  const AvoidAreaUI({super.key});

  @override
  State<AvoidAreaUI> createState() => _MyAppState();
}

class _MyAppState extends State<AvoidAreaUI> {
  MethodChannel avoidAreaMethod =
      const MethodChannel('flutter_avoid_area_method');
  EventChannel avoidAreaEvent = const EventChannel('flutter_avoid_area_event');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Avoid Area UI'),
        ),
        body: SafeArea(
            child: SingleChildScrollView(
                child: Column(
          children: [
            const TextField(
              decoration: InputDecoration(labelText: '点击拉起输入法，获取软键盘避让区域数据'),
            ),
            MaterialButton(
                color: Colors.blue,
                minWidth: double.infinity,
                height: 30,
                child: const Text("TYPE_SYSTEM"),
                onPressed: () async {
                  final windowAvoidArea = await AvoidAreaApi.getWindowAvoidArea(
                      AvoidAreaType.system);
                  if (kDebugMode) {
                    print(
                        'avoidAreaMethod.system = ${json.encode(windowAvoidArea)}');
                  }
                }),
            MaterialButton(
                color: Colors.blue,
                minWidth: double.infinity,
                height: 30,
                child: const Text("TYPE_CUTOUT"),
                onPressed: () async {
                  final windowAvoidArea = await AvoidAreaApi.getWindowAvoidArea(
                      AvoidAreaType.cutout);
                  if (kDebugMode) {
                    print(
                        'avoidAreaMethod.cutout = ${json.encode(windowAvoidArea)}');
                  }
                }),
            MaterialButton(
                color: Colors.blue,
                minWidth: double.infinity,
                height: 30,
                child: const Text("TYPE_SYSTEM_GESTURE"),
                onPressed: () async {
                  final windowAvoidArea = await AvoidAreaApi.getWindowAvoidArea(
                      AvoidAreaType.systemGesture);
                  if (kDebugMode) {
                    print(
                        'avoidAreaMethod.systemGesture = ${json.encode(windowAvoidArea)}');
                  }
                }),
            MaterialButton(
                color: Colors.blue,
                minWidth: double.infinity,
                height: 30,
                child: const Text("TYPE_KEYBOARD"),
                onPressed: () async {
                  final windowAvoidArea = await AvoidAreaApi.getWindowAvoidArea(
                      AvoidAreaType.keyboard);
                  if (kDebugMode) {
                    print(
                        'avoidAreaMethod.keyboard = ${json.encode(windowAvoidArea)}');
                  }
                }),
            MaterialButton(
                color: Colors.blue,
                minWidth: double.infinity,
                height: 30,
                child: const Text("TYPE_NAVIGATION_INDICATOR"),
                onPressed: () async {
                  final windowAvoidArea = await AvoidAreaApi.getWindowAvoidArea(
                      AvoidAreaType.navigationIndicator);
                  if (kDebugMode) {
                    print(
                        'avoidAreaMethod.navigationIndicator = ${json.encode(windowAvoidArea)}');
                  }
                }),
            MaterialButton(
                color: Colors.blue,
                minWidth: double.infinity,
                height: 30,
                child: const Text("addAvoidAreaListener"),
                onPressed: () {
                  AvoidAreaApi.addAvoidAreaListener((event) {
                    if (kDebugMode) {
                      print('receiveBroadcastStream event = $event');
                    }
                  });
                }),
            MaterialButton(
                color: Colors.blue,
                minWidth: double.infinity,
                height: 30,
                child: const Text("removeAvoidAreaListener"),
                onPressed: () {
                  AvoidAreaApi.removeAvoidAreaListener();
                }),
          ],
        ))));
  }
}
